package org.blog.logic;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.blog.database.connection.ConnectionManager;

import blog.forms.Blog;
import blog.forms.Comment;
import blog.forms.Entry;
import blog.forms.User;

public class BlogLogic {

	static private BlogLogic blogLogic;
	
	static public BlogLogic getInstance() {
		if (blogLogic == null) {
			blogLogic = new BlogLogic();
		}
		return blogLogic;
	}
	
	public void add(Blog blog){

		Connection conn = ConnectionManager.getManager().getConnection();
		PreparedStatement st = null;
		
		try {
			
			st = conn.prepareStatement("INSERT INTO blogi (idUzyt, nazwa, opis)"
					+ " VALUES (?,?,?)");

			st.setInt(1, blog.getUserId());
			st.setString(2, blog.getName());
			st.setString(3, blog.getDescription());

			st.executeUpdate();
			st.close();

		} catch (SQLException e) {
			e.printStackTrace();
		} 
	
	}
	public void addEntry(int blogId, Entry entry) {
		
		Connection conn = ConnectionManager.getManager().getConnection();
		PreparedStatement st = null;
		
		try {
			
			st = conn.prepareStatement("INSERT INTO wpisy (idBloga, tytul, tresc, dataUtw)"
					+ " VALUES (?,?,?,?)");

			st.setInt(1, blogId);
			st.setString(2, entry.getTitle());
			st.setString(3, entry.getText());
			st.setDate(4, entry.getDate());

			st.executeUpdate();
			st.close();

		} catch (SQLException e) {
			e.printStackTrace();
		} 
	
	}
	public void addComment(int entryId, int userId, Comment comment){

		Connection conn = ConnectionManager.getManager().getConnection();
		PreparedStatement st = null;
		
		try {
			
			st = conn.prepareStatement("INSERT INTO komentarze (idWpisu, idUzyt, tresc)"
					+ " VALUES (?,?,?)");

			st.setInt(1, entryId);
			st.setInt(2, userId);
			st.setString(3, comment.getText());

			st.executeUpdate();
			st.close();

		} catch (SQLException e) {
			e.printStackTrace();
		} 
	
	}
	public List<Comment> getComments(int entryId) {
		
		List<Comment> comments = new ArrayList<Comment>();
		
		Connection conn = ConnectionManager.getManager().getConnection();
		PreparedStatement st = null;
		ResultSet resultSet = null;
		try {
			
			st = conn.prepareStatement("select * from komentarze where idWpisu = ?");
			
			st.setInt(1, entryId);
			
			resultSet = st.executeQuery();
			if(resultSet != null) {
				while(resultSet.next()) {
					Comment comment = new Comment();
					initComment(comment, resultSet);
					comments.add(comment);
				}
			}

			st.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return comments;
	}
	public List<Blog> getBlogs(User user) {
		
		List<Blog> blogs = new ArrayList<Blog>();
		
		Connection conn = ConnectionManager.getManager().getConnection();
		PreparedStatement st = null;
		ResultSet resultSet = null;
		try {
			
			st = conn.prepareStatement("select * from blogi where idUzyt = ?");
			
			st.setInt(1, user.getId());
			
			resultSet = st.executeQuery();
			if(resultSet != null) {
				while(resultSet.next()) {
					Blog blog = new Blog();
					initBlog(blog, resultSet);
					blogs.add(blog);
				}
			}

			st.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return blogs;
	}
	
	public List<Entry> getEntries(int blogId) {
		
		List<Entry> entries = new ArrayList<Entry>();
		
		Connection conn = ConnectionManager.getManager().getConnection();
		PreparedStatement st = null;
		ResultSet resultSet = null;
		try {
			
			st = conn.prepareStatement("select * from wpisy where idBloga = ?");
			
			st.setInt(1, blogId);
			
			resultSet = st.executeQuery();
			if(resultSet != null) {
				while(resultSet.next()) {
					Entry entry = new Entry();
					initEntry(entry, resultSet);
					entries.add(entry);
				}
			}

			st.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return entries;
	}
	public Entry getEntry(int id) {
		
		Entry entry = null;
		
		Connection conn = ConnectionManager.getManager().getConnection();
		PreparedStatement st = null;
		ResultSet resultSet = null;
		try {
			
			st = conn.prepareStatement("select * from wpisy where idWpisu = ?");
			
			st.setInt(1, id);
			
			resultSet = st.executeQuery();
			if(resultSet != null) {
				if(resultSet.next()) {
					entry = new Entry();
					initEntry(entry, resultSet);
				}
			}

			st.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return entry;
	}
	public List<Blog> getBlogs() {
		
		List<Blog> blogs = new ArrayList<Blog>();
		
		Connection conn = ConnectionManager.getManager().getConnection();
		PreparedStatement st = null;
		ResultSet resultSet = null;
		try {
			
			st = conn.prepareStatement("select * from blogi");
			
			resultSet = st.executeQuery();
			if(resultSet != null) {
				while(resultSet.next()) {
					Blog blog = new Blog();
					initBlog(blog, resultSet);
					blogs.add(blog);
				}
			}

			st.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return blogs;
	}
	public Blog getBlog(int id) {
		
		Blog blog = null;
		
		Connection conn = ConnectionManager.getManager().getConnection();
		PreparedStatement st = null;
		ResultSet resultSet = null;
		try {
			
			st = conn.prepareStatement("select * from blogi where idBloga = ?");
			
			st.setInt(1, id);
			
			resultSet = st.executeQuery();
			if(resultSet != null) {
				if(resultSet.next()) {
					blog = new Blog();
					initBlog(blog, resultSet);
				}
			}

			st.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return blog;
	}
	private static void initBlog(Blog b, ResultSet rs) {		
		try {
			b.setId(rs.getInt("idBloga"));
			b.setUserId(rs.getInt("idUzyt"));
			b.setName(rs.getString("nazwa"));
			b.setDescription(rs.getString("opis"));
			b.setEntries((ArrayList<Entry>) getInstance().getEntries(b.getId()));
			
		} catch(SQLException e) {
			b = null;
		}
	}
	private static void initEntry(Entry e, ResultSet rs) {		
		try {
			e.setId(rs.getInt("idWpisu"));
			e.setBlogId(rs.getInt("idBloga"));
			e.setTitle(rs.getString("tytul"));
			e.setText(rs.getString("tresc"));
			e.setDate(rs.getDate("dataUtw"));
			e.setComments((ArrayList<Comment>) getInstance().getComments(e.getId()));
		
		} catch(SQLException ex) {
			e = null;
		}
	}
	private static void initComment(Comment c, ResultSet rs) {		
		try {
			
			c.setId(rs.getInt("idKoma"));
			c.setUserId(rs.getInt("idUzyt"));
			c.setEntryId(rs.getInt("idWpisu"));
			c.setText(rs.getString("tresc"));
			
		} catch(SQLException ex) {
			c = null;
		}
	}
	public static void main(String[] as) {
		 
		 
	}
}
